package com.dongwu.security.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.dongwu.manager.entity.TemplateType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.dongwu.common.entity.BaseEntity;
import com.dongwu.common.utils.Collections3;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Joiner;


@Entity
@Table(name = "sys_user")
public class User extends BaseEntity<String>{

    private String name;
    
	private String loginName;
    
    private String email;
    
    private String password;
    
    private String postName;
    
    private String outSn;

	private String type;

	@ElementCollection
    @JoinTable(name = "sys_user_data_permission", joinColumns = { @JoinColumn(nullable = false, name = "user_id") }) 
    @OrderColumn(name = "idx") 
    @Column(name = "shop_id", nullable = false) 
    private List<String> dataPermission= new ArrayList<String>();

	@ManyToMany(cascade = {}, fetch = FetchType.LAZY)
    @JoinTable(name = "sys_user_role",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "roles_id")})
    @Fetch(value = FetchMode.SUBSELECT)
    private List<Role> roles;

	@Deprecated
    @JsonIgnore
    @ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinTable(name = "sys_group_user",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "group_id")})
    @Fetch(value = FetchMode.SUBSELECT)
    private List<Group> groups;
    
    @ElementCollection
    @JoinTable(name = "sys_user_group", joinColumns = { @JoinColumn(nullable = false, name = "user_id") }) 
    @OrderColumn(name = "idx") 
    @Column(name = "group_id", nullable = false) 
    private List<String> userGroups;
    
	@Deprecated
	@JsonIgnore
	@ManyToMany(cascade = {}, fetch = FetchType.LAZY)
	@JoinTable(name = "template_type_user",
			joinColumns = {@JoinColumn(name = "user_id")},
			inverseJoinColumns = {@JoinColumn(name = "template_type_id")})
	@Fetch(value = FetchMode.SUBSELECT)
	private List<TemplateType> templateTypes;

	@ElementCollection
	@JoinTable(name = "sys_user_template_type", joinColumns = { @JoinColumn(nullable = false, name = "user_id") })
	@OrderColumn(name = "idx")
	@Column(name = "template_type_id", nullable = false)
	private List<String> userTemplateTypes;

    
    public String getOutSn() {
		return outSn;
	}


	public List<String> getDataPermission() {
		return dataPermission;
	}

	@Transient
	public String getDataPermissionStr() {
		if (Collections3.isEmpty(dataPermission)) {
			return "";
		}
		
		return Joiner.on(",").join(dataPermission);
	}

	public String getEmail() {
        return email;
    }

    public List<Group> getGroups() {
		return groups;
	}


	public String getLoginName() {
		return loginName;
	}    


    public String getName() {
        return name;
    }  
    
    public String getPassword() {
        return password;
    }

    public String getPostName() {
		return postName;
	}

    public List<Role> getRoles() {
        return roles;
    }

	public String getType() {
		return type;
	}

	public List<String> getUserGroups() {
		return userGroups;
	}

    @Transient
	public String getUserGroupsStr() {
		if (Collections3.isEmpty(userGroups)) {
			return "";
		}
		
		return Joiner.on(",").join(userGroups);
	}

	@Transient
	public String getUserTemplateTypeStr() {
		if (Collections3.isEmpty(userTemplateTypes)) {
			return "";
		}

		return Joiner.on(",").join(userTemplateTypes);
	}


    public void setOutSn(String outSn) {
		this.outSn = outSn;
	}

	public void setDataPermission(List<String> dataPermission) {
		this.dataPermission = dataPermission;
	}

	public void setEmail(String email) {
        this.email = email;
    }

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public void setName(String name) {
        this.name = name;
    }

	public void setPassword(String password) {
        this.password = password;
    }

	public void setPostName(String postName) {
		this.postName = postName;
	}

	public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

	public void setType(String type) {
		this.type = type;
	}

	public void setUserGroups(List<String> userGroups) {
		this.userGroups = userGroups;
	}

	public List<TemplateType> getTemplateTypes() {
		return templateTypes;
	}

	public void setTemplateTypes(List<TemplateType> templateTypes) {
		this.templateTypes = templateTypes;
	}

	public List<String> getUserTemplateTypes() {
		return userTemplateTypes;
	}

	public void setUserTemplateTypes(List<String> userTemplateTypes) {
		this.userTemplateTypes = userTemplateTypes;
	}

	@Override
	public String toString() {
		return "User [name=" + name + ", loginName=" + loginName + ", email=" + email + "]";
	}
}
